package com.sz10.p2p.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.sz10.p2p.Constants;
import com.sz10.p2p.mapper.LoanInfoMapper;
import com.sz10.p2p.model.LoanInfo;
import com.sz10.p2p.service.LoanInfoService;
import com.sz10.p2p.vo.PaginationVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
@Service(interfaceClass = LoanInfoService.class, version = "1.0.0", timeout = 15000)
public class LoanInfoServiceImpl implements LoanInfoService {

    @Resource
    private LoanInfoMapper loanInfoMapper;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 获取平台历史年化收益率
     *
     * @return
     */

    @Override
    public Double queryHistoryAvgRate() {

        //修改redis序列号方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //到redis中获取数据
        Double historyAvgRate = (Double) redisTemplate.opsForValue().get(Constants.HISTORY_AVG_RATE);
        /**
         * 双重验证同步代码块
         */
        if (!ObjectUtils.allNotNull(historyAvgRate)) {
            synchronized (this) {
                historyAvgRate = (Double) redisTemplate.opsForValue().get(Constants.HISTORY_AVG_RATE);

                //1.redis中有数据，直接返回
                if (!ObjectUtils.allNotNull(historyAvgRate)) {
                    historyAvgRate = loanInfoMapper.selectHistoryAvgRate();

                    //2.redis中无数据.查询数据库并向redis中储存数据
                    redisTemplate.opsForValue().set(Constants.HISTORY_AVG_RATE, historyAvgRate, 15, TimeUnit.DAYS);
                }
            }
        }
        return historyAvgRate;
    }

    /**
     * 获取平台累计成交金额
     *
     * @return
     */
    @Override
    public Double queryAllBidMoney() {
        /**
         * 双重验证+同步代码块
         */
        Double allBidMoney = (Double) redisTemplate.opsForValue().get(Constants.ALL_BID_MONEY);
        if (!ObjectUtils.allNotNull(allBidMoney)) {
            synchronized (this) {
                allBidMoney = (Double) redisTemplate.opsForValue().get(Constants.ALL_BID_MONEY);
                if (!ObjectUtils.allNotNull(allBidMoney)) {
                    allBidMoney = loanInfoMapper.selectAllBidMoney();
                    redisTemplate.opsForValue().set(Constants.ALL_BID_MONEY, allBidMoney);
                }
            }
        }
        return allBidMoney;
    }

    /**
     * 根据产品类型查询产品list集合
     *
     * @param paramMap
     * @return
     */

    @Override
    public List<LoanInfo> queryLoanInfoListByProductType(Map<String, Object> paramMap) {
        return loanInfoMapper.selectLoanInfoListByProductType(paramMap);
    }

    /**
     * 分页查询产品列表
     *
     * @param paramMap
     * @return
     */
    @Override
    public PaginationVO<LoanInfo> queryLoanInfoListByPage(Map<String, Object> paramMap) {
        PaginationVO<LoanInfo> PaginationVO = new PaginationVO<>();

        //查询List<LoanInfo>
        List<LoanInfo> loanInfosList = loanInfoMapper.selectLoanInfoListByProductType(paramMap);

        //查询LoanInfo的总记录数
        Long totalPage = loanInfoMapper.selectLoanInfoTotalPage(paramMap);

        //将查询到的数据封装到PageNationVO类中
        PaginationVO.setDataList(loanInfosList);
        PaginationVO.setTotal(totalPage);

        return PaginationVO;
    }
}
